﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DTBuilderLib;
using System.IO;

namespace t_DTBuilderLib
{
    /// <summary>
    /// Summary description for t_ItemsFileIO
    /// </summary>
    [TestClass]
    public class t_ItemsFileIO
    {
        public t_ItemsFileIO()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ReadCorrectItems()
        {
            Collection c = DTBuilderLib.BinaryIO.ItemsFileIO.Load(new FileInfo("..\\..\\..\\t_DTBuilderLib\\items.bin"));

            Assert.IsTrue(c != null, "Return was null!");
            Assert.IsTrue(c.Version == 2, "Version is incorrect");
            Assert.IsTrue(c.UseStringsFile == 0, "string file is incorrect");
            Assert.IsTrue(c.MinLevel == 0, "Min level not right");
            Assert.IsTrue(c.MaxLevel == 8, "Max level not right");
            Assert.IsTrue(c.PageSizeLog2 == 8, "Page size is not right");
            Assert.IsTrue(c.PageQuality == 1, "Page quality is not right");
            Assert.IsTrue(c.ItemCount == 4, "Item count is not right");
            Assert.IsTrue(c.NextItemId == 4, "Next item id is not right");

            Assert.IsTrue(c.Items != null, "Items are null");
            Assert.IsTrue(c.Thumbnails != null, "Thumb nails are null!");

            Assert.IsTrue(c.Items.Length == 1, "Items length is not 1");
            Assert.IsTrue(c.Thumbnails.Length == 1, "Thums are not length 1");

            Assert.IsTrue(c.Items[0] != null, "Items[0] is null");
            Assert.IsTrue(c.Thumbnails[0] != null, "Thums[0] is null");

            Assert.IsTrue(c.Items[0].Item != null, "Items[0].Item is null!");
            Assert.IsTrue(c.Thumbnails[0].Thumbnail != null, "Thums[0].Thums is null!");

            Assert.IsTrue(c.Items[0].Item.Length == 4, "Item list is not right");
            Assert.IsTrue(c.Thumbnails[0].Thumbnail.Length == 4, "Thums list is not right");

            for (int i = 0; i < 4; i++)
            {
                Assert.IsTrue(c.Items[0].Item[i].Id == i, "Item ID is not right");
                Assert.IsTrue(c.Items[0].Item[i].Thumb == i, "Item Thumb reference is not right");
                Assert.IsTrue(c.Items[0].Item[i].Relative == 1, "Relative is not 1!");
                Assert.IsTrue(c.Items[0].Item[i].Type == "ImagePixelSource", "Image type is not right");
                Assert.IsTrue(c.Items[0].Item[i].Init == "collectionattempt_images\\talk-" + (i+1).ToString("000") + ".sdi", "Image relative path is not right");

                Assert.IsTrue(c.Thumbnails[0].Thumbnail[i].Id == i, "Thumb name iD is not right");
                Assert.IsTrue(c.Thumbnails[0].Thumbnail[i].MinLevel == 0, "Min level is not right");
                Assert.IsTrue(c.Thumbnails[0].Thumbnail[i].MaxLevel == 8, "Max level is not right");
                Assert.IsTrue(Math.Abs(c.Thumbnails[0].Thumbnail[i].SizeX - 0.732421875) < 0.001, "Size X is not right");
                Assert.IsTrue(Math.Abs(c.Thumbnails[0].Thumbnail[i].SizeY - 0.5493164063) < 0.001, "Size X is not right");
            }

            /// No idea. Not filled in??
            Assert.IsTrue(c.PageFormat == "\0\0\0\0\0\0ð?", "page format is not right");
        }

        [TestMethod]
        public void WriteItemsFile()
        {
            FileInfo original = new FileInfo("..\\..\\..\\t_DTBuilderLib\\items.bin");
            FileInfo newguy = new FileInfo (".\\items.bin");
            Collection c = DTBuilderLib.BinaryIO.ItemsFileIO.Load(original);
            DTBuilderLib.BinaryIO.ItemsFileIO.Save(c, newguy);

            Assert.IsTrue(TestUtilities.CompareBinaryFiles(original, newguy), "The written binary file is not the same as the original one!");
        }
    }
}
